package com.tl.sun.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.helper.UIHelper;
import com.tl.sun.model.BaseModel;
import com.tl.sun.model.PackageEntity;
import com.tl.sun.ui.activity.PackageTypeActivity;
import com.tl.sun.ui.adapter.PackageAdapter;
import com.tl.sun.utils.ToastUtil;
import com.tl.sun.utils.core.EmptyUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/11.
 */

public class PayFragment extends Fragment {

    @Bind(R.id.rv_pay_list)
    RecyclerView mRvPayList;
    @Bind(R.id.ll_combo_one)
    LinearLayout mLlComboOne;
    @Bind(R.id.ll_combo_two)
    LinearLayout mLlComboTwo;
    private View rootView;
    private List<PackageEntity> packageList = new ArrayList<>();
    private PackageAdapter mPackageAdapter;
    private String mType;

    public static PayFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(UIHelper.TYPE, type);
        PayFragment fragment = new PayFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isNotEmpty(getArguments())) {
            mType = getArguments().getString(UIHelper.TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_pay, container, false);
            ButterKnife.bind(this, rootView);
            init();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }


    public void init() {
        if (mType.equals("1")) {
            mLlComboOne.setVisibility(View.VISIBLE);
        } else {
            mLlComboTwo.setVisibility(View.VISIBLE);
        }

        mPackageAdapter = new PackageAdapter(packageList);
        mRvPayList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvPayList.setAdapter(mPackageAdapter);
        mRvPayList.setNestedScrollingEnabled(false);
        mRvPayList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                switch (view.getId()) {
                    case R.id.tv_package_submit:
                        showPackagePayPage(getActivity(), packageList.get(position).getId() + "", packageList.get(position).getName());
                        break;
                }
            }
        });
        getPackageList();
    }

    /**
     * 选择支付方式
     *
     * @param activity
     */
    public static void showPackagePayPage(Activity activity, String id, String type) {
        Intent intent = new Intent(activity, PackageTypeActivity.class);
        intent.putExtra(UIHelper.PACKAGE_ID, id);
        intent.putExtra(UIHelper.TYPE, type);
        activity.startActivity(intent);
    }

    private void getPackageList() {
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(getActivity()))
                .post(AppContext.getInstance().getBaseUrl() + "buy/package_new")
                .params("pak_origin", "1")
                .params("type", mType)
                .tag("pay")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        Type packageType = new TypeToken<BaseModel<List<PackageEntity>>>() {
                        }.getType();
                        BaseModel<List<PackageEntity>> packList;
                        try {
                            packList = gson.fromJson(s, packageType);
                        } catch (JsonSyntaxException e) {
                            packList = gson.fromJson(s, BaseModel.class);
                        }
                        if (packList.getRet().equals("0")) {
                            if (packList.getData().size() >= 4) {
                                for (int i = 0; i < 3; i++) {
                                    packageList.add(packList.getData().get(i));
                                }
                            }else{
                                packageList = packList.getData();
                            }
                            mPackageAdapter.setNewData(packageList);
                        } else {
                            ToastUtil.showToast(packList.getMsg());
                        }

                    }
                });
    }

}
